﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CLibArtreve.dal;
using System.Configuration;
using CLibArtreve.models;
using System.Data;

namespace CLibArtreve.bll
{
    public class UserBLL
    {
        #region VARIABLES
        private string _connectionString;        
        private UserDAL _adapter;
        #endregion

        #region PROPERTIES
        public string ConnectionString
        {
            get {
                if (_connectionString == null)
                    _connectionString = ConfigurationManager.ConnectionStrings["ARTREVEConnectionString"].ConnectionString;
                return _connectionString; 
            }
            set { _connectionString = value; }
        }
        public UserDAL Adapter
        {
            get
            {
                if (_adapter == null)
                    _adapter = new UserDAL(ConnectionString);
                return _adapter;
            }
        }
        #endregion

        #region CONSTRUCTOR
        public UserBLL(string connectionString)
        {
            ConnectionString = connectionString;
        }
        #endregion

        #region OVERRIDE METHODS
        public override string ToString()
        {
            return "BUSINESS LOGIC LAYER: " + this.GetType().ToString();
        }
        #endregion

        #region GET METHODS
        public IList<User> GetAllUsers()
        {
            try
            {
                //GET DATASET
                DataSet ds = Adapter.GetAllUsers();
                //MAKE A LIST
                IList<User> list = new List<User>();
                //LOOP THROUGH ROWS OF DATASET
                foreach (DataRow row in ds.Tables[0].Rows)
                {
                    list.Add(GenerateUserFromDataRow(row));
                }
                //RETURN LIST
                return list;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public User GetUserById(Int64? id)
        {
            try
            {
                //GET DATASET
                DataSet ds = Adapter.GetUserById(id);

                if (ds != null && ds.Tables[0].Rows.Count > 0)
                {
                    DataRow row = ds.Tables[0].Rows[0];
                    return GenerateUserFromDataRow(row);
                }
                return null;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /*public User GetUserByUserName(string userName)
        {
            try
            {
                //GET DATASET
                DataSet ds = Adapter.ge (userName);

                if (ds != null && ds.Tables[0].Rows.Count > 0)
                {
                    DataRow row = ds.Tables[0].Rows[0];
                    return GenerateUserFromDataRow(row);
                }
                return null;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }*/

        public User GetUserByEmail(string email)
        {
            try
            {
                //GET DATASET
                DataSet ds = Adapter.GetUserByEmail(email);

                if (ds != null && ds.Tables[0].Rows.Count > 0)
                {
                    DataRow row = ds.Tables[0].Rows[0];
                    return GenerateUserFromDataRow(row);
                }
                return null;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        #region INSERT METHODS
        [System.ComponentModel.DataObjectMethodAttribute
            (System.ComponentModel.DataObjectMethodType.Insert, true)]
        public int InsertUser(User user)
        {
            try
            {
                return Adapter.InsertUser(user.Firstname, user.Givenname, user.Nickname, user.Email, user.Password,
                    user.Dob, user.SecretquestionId, user.Secretanswer);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message, ex);
            }
        }
        #endregion

        #region UPDATE METHODS
        [System.ComponentModel.DataObjectMethodAttribute
            (System.ComponentModel.DataObjectMethodType.Insert, true)]
        public int Save(User user)
        {
            if (user.Id != null)
            {
                return UpdateUser(user);
            }
            else
            {
                return InsertUser(user);
            }
        }

        public int UpdateUser(User user)
        {
            try
            {    
                return Adapter.UpdateUser(user.Id, user.Firstname, user.Givenname, user.Nickname, user.Email, user.Password, 
                    user.Dob, user.SecretquestionId, user.Secretanswer);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message, ex);
            }
        }

        public int UpdateTokens(Int64 UserId, int Tokens)
        {
            try
            {
                return Adapter.UpdateTokens(UserId, Tokens);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message, ex);
            }
        }
        #endregion

        #region UTILITIES
        private User GenerateUserFromDataRow(DataRow row)
        {
            User obj = new User();
            obj.Id = (Int64)row["user_id"];
            obj.Nickname = (string)row["user_nickname"];
            obj.Firstname = (string)row["user_firstname"];
            obj.Givenname = (string)row["user_givenname"];
            obj.Email = (string)row["user_email"];
            obj.Password = (string)row["user_password"];
            if (row["user_dob"] != DBNull.Value)
                obj.Dob = (DateTime?)row["user_dob"];
            obj.Creationdate = (DateTime)row["user_creationdate"];
            obj.Activationdate = (DateTime)row["user_activationdate"];
            if (row["user_deleteddate"] != DBNull.Value)
                obj.Deleteddate = (DateTime?)row["user_deleteddate"];
            if (row["user_modifieddate"] != DBNull.Value)
                obj.Modifieddate = (DateTime?)row["user_modifieddate"];
            if (row["user_lastlogindate"] != DBNull.Value)
                obj.Lastlogindate = (DateTime?)row["user_lastlogindate"];
            if (row["user_lockeddate"] != DBNull.Value)
                obj.Lockeddate = (DateTime?)row["user_lockeddate"];
            obj.SecretquestionId = Convert.ToInt64(row["user_secretquestion_id"]);
            if(row["user_secretanswer"] != DBNull.Value)
                obj.Secretanswer = (string)row["user_secretanswer"];
            if (row["user_activationtoken"] != DBNull.Value)
                obj.Activationtoken = (string)row["user_activationtoken"];
            obj.Tokens = (int)row["user_tokens"];

            return obj;
        }
        #endregion
    }
}
